<template>
  <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="800px"
      top="5vh"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :before-close="handleClose"
  >
    <el-form :model="form" :rules="rules" ref="form" label-width="120px">
     <el-row :gutter="12" :span="12">
       <el-form-item label="用户ID" prop="user_id">
         <el-input v-model="form.user_id" placeholder="请输入用户ID"></el-input>
       </el-form-item>
       <el-form-item label="申请产品" prop="product_id">
         <el-select v-model="form.product_id" placeholder="请选择申请产品" clearable style="width: 100%">
           <el-option
               v-for="item in productOptions"
               :key="item.id"
               :label="item.name"
               :value="item.id">
           </el-option>
         </el-select>
       </el-form-item>
       <el-form-item label="联系人" prop="contact_person">
         <el-input v-model="form.contact_person" placeholder="请输入联系人"></el-input>
       </el-form-item>
       <el-form-item label="联系方式" prop="contact_phone">
         <el-input v-model="form.contact_phone" placeholder="请输入联系方式"></el-input>
       </el-form-item>
       <el-form-item label="社保缴纳基数" prop="social_security_base">
         <el-input-number v-model="form.social_security_base" :min="0" :precision="2"></el-input-number>
       </el-form-item>
       <el-form-item label="公积金缴纳基数" prop="housing_fund_base">
         <el-input-number v-model="form.housing_fund_base" :min="0" :precision="2"></el-input-number>
       </el-form-item>
       <el-form-item label="工资区间" prop="salary_range">
         <el-input v-model="form.salary_range" placeholder="请输入工资区间"></el-input>
       </el-form-item>
       <el-form-item label="申请填报数据" prop="application_data">
         <el-input v-model="form.application_data" type="textarea" :rows="3" placeholder="请输入申请填报数据"></el-input>
       </el-form-item>
       <el-form-item label="审核状态" prop="review_status">
         <el-select v-model="form.review_status" placeholder="请选择审核状态" clearable style="width: 100%">
           <el-option label="待审核" :value="1"></el-option>
           <el-option label="审核通过" :value="2"></el-option>
           <el-option label="审核拒绝" :value="3"></el-option>
         </el-select>
       </el-form-item>
       <el-form-item label="审核备注" prop="review_remark">
         <el-input v-model="form.review_remark" type="textarea" :rows="3" placeholder="请输入审核备注"></el-input>
       </el-form-item>
       <el-form-item label="分配的业务员" prop="assignee">
         <el-input v-model="form.assignee" placeholder="请输入分配的业务员"></el-input>
       </el-form-item>
     </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { http } from "@/api";

export default {
  name: "LoanApplicationAdd",
  data() {
    return {
      dialogVisible: false,
      dialogTitle: "新增申请记录",
      isEdit: false,
      productOptions: [],
      form: {
        user_id: "",
        product_id: "",
        contact_person: "",
        contact_phone: "",
        social_security_base: 0,
        housing_fund_base: 0,
        salary_range: "",
        application_data: "",
        review_status: 1,
        review_remark: "",
        assignee: ""
      },
      rules: {
        user_id: [
          { required: true, message: "请输入用户ID", trigger: "blur" }
        ],
        product_id: [
          { required: true, message: "请选择申请产品", trigger: "change" }
        ],
        contact_person: [
          { required: true, message: "请输入联系人", trigger: "blur" },
          { max: 50, message: "联系人不能超过50个字符", trigger: "blur" }
        ],
        contact_phone: [
          { required: true, message: "请输入联系方式", trigger: "blur" },
          { max: 20, message: "联系方式不能超过20个字符", trigger: "blur" }
        ],
        salary_range: [
          { max: 50, message: "工资区间不能超过50个字符", trigger: "blur" }
        ],
        review_remark: [
          { max: 500, message: "审核备注不能超过500个字符", trigger: "blur" }
        ],
        assignee: [
          { max: 64, message: "分配的业务员不能超过64个字符", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getProducts();
  },
  methods: {
    // 获取产品列表
    async getProducts() {
      try {
        const res = await http.product.list();
        this.productOptions = res.data || [];
      } catch (error) {
        console.error(error);
        this.$message.error("获取产品列表失败");
      }
    },
    open(row) {
      this.dialogVisible = true;
      if (row) {
        this.isEdit = true;
        this.dialogTitle = "编辑申请记录";
        http.loanApplication.read(row.id).then(res => {
          this.form = {
            ...res.data
          }
        })
      } else {
        this.isEdit = false;
        this.dialogTitle = "新增申请记录";
        this.form = {
          user_id: "",
          product_id: "",
          contact_person: "",
          contact_phone: "",
          social_security_base: 0,
          housing_fund_base: 0,
          salary_range: "",
          application_data: "",
          review_status: 1,
          review_remark: "",
          assignee: ""
        };
      }
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    handleClose() {
      this.dialogVisible = false;
      this.$refs.form.resetFields();
    },
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            if (this.isEdit) {
              await http.loanApplication.update(this.form.id, this.form);
              this.$message.success("更新成功");
            } else {
              await http.loanApplication.create(null, this.form);
              this.$message.success("新增成功");
            }
            this.handleClose();
            this.$emit("get-page");
          } catch (error) {
            console.error(error);
            this.$message.error("操作失败");
          }
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
.el-dialog {
  max-height: 90vh;
  overflow: auto;
}
.el-dialog__body {
  max-height: calc(90vh - 120px);
  overflow-y: auto;
}
.tip-text {
  font-size: 12px;
  color: #909399;
  margin-top: 5px;
}
</style> 